/**
 * 链栈
 * 能够动态的分配空间大小，解决 顺序栈 的不足
 * 缺点是，操作实现更麻烦点
 */

  /**
  * 节点数据类型
  */
class Node {
    constructor (data = null, next = null) {
        this.data = data
        this.next = next
    }
}

/**
 * 指定 链表头部 作为栈顶更加方便
 */
class StackChain {
    constructor () {
        let head = new Node()
        this.top = head
    }
    push (val) {
        let status = 'overflow'
        let node = new Node(val)
        if (node) {
            node.next = this.top.next
            this.top.next = node
            status = 'success'
        }
        return status
    }
    pop () {
        let status = 'success'
        if (this.empty()) {
            status = 'underflow'
        } else {
            let node = this.top.next
            this.top.next = node.next
            node = null
            status = 'success'
        }
        return status
    }
    top () {
        let status = 'success'
        if (this.empty()) {
            status = 'underflow'
        } else {
            console.log(this.top.next.data)
        }
        return status
    }
    empty () {
        return this.top.next === null
    }
    /**
     * 清空数据
     */
    clear () {
        let node = this.top.next
        while (node) {
            this.top.next = node.next
            node = null
            node = this.top.next
        }
        this.top.next = null
    }
}

module.export = StackChain

